import qs from 'qs';
import Vue from 'vue';
import Axios from 'axios';

Vue.$http = Vue.prototype.$http = Axios;

// Axios.defaults.baseURL = '/virtual';
// Axios.defaults.timeout = 10000;
// Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

// let request = Axios.create();

// request.interceptors.request.use(
//     config => {
//         return config;
//     },
//     error => {
//         return Promise.error(error);
//     }
// );
// request.interceptors.response.use(
//     response => {
//         return Promise.resolve(response);
//     },
//     error => {
//         return Promise.reject(error.response);
//     }
// );

let request = {
    beforeRequest: null,
    send(url, params = {}, opts = {}) {
        console.log('[http] send: ', url, params, opts);
        let param = {
            url: url,
            method: opts.method || 'GET',
            headers: {},
            validateStatus: null, // all server return status is resolve
            params: {}
        };
        if (param.method.toLowerCase() === 'post') param = qs.stringify(param);
        return Vue.$http.request(param);
    },
    afterRequest: null
};
Vue.$request = Vue.prototype.$request = request;

export default request;
